<?php


namespace app\common\validate;

use app\common\exception\ParameterFailedException;
use think\Validate;

class CommonValidate extends Validate
{
    protected $rule    = [
        'id|ID'      => 'require|integer|between:1,100000000',
        'page|页数'    => 'integer|between:1,100000000',
        'limit|每页条数' => 'integer|between:1,100000',
        'ids'        => 'enumIds',
    ];
    protected $message = [];
    protected $scene   = [
        'paginate'      => [
            'page',
            'limit',
        ],
        'printTihuodan' => ['ids'],
    ];

    /**
     * 验证
     * @param bool $scene
     * @param array $param
     * @param bool $batch
     * @return bool
     * @throws ParameterFailedException
     */
    public function goCheck($scene = false, array $param = [], $batch = false): bool
    {
        $r_params = empty($param) ? \request()->param() : $param;
        if ($scene) {
            $this->scene($scene);
        }
        if ($batch) {
            $this->batch();
        }
        if (!$this->check($r_params)) {
            //            halt($this->error);
            throw new ParameterFailedException(['msg' => $this->error]);
        }
        return true;
    }

    /**
     * 检测是否包含中文
     * @param $value
     * @return bool
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 10:46
     */
    protected function notContainsChinese($value): bool
    {
        if (preg_match("/([\x81-\xfe][\x40-\xfe])/", $value)) {
            return false;
        }
        return true;
    }

    /**
     * @param $value
     * @return bool
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 10:46
     */
    protected function enumZeroOne($value): bool
    {
        try {
            if (in_array($value, [
                '0',
                '1',
            ], true)) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * @param $ids
     * @return bool
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 10:46
     */
    protected function enumIds($ids): bool
    {
        try {
            if (!is_array($ids)) {
                $ids = explode(',', $ids);
            }
            if (count($ids) !== count(array_unique($ids))) {
                return false;
            }
            $ids = array_unique($ids);

            foreach ($ids as $id) {
                if (filter_var($id, FILTER_VALIDATE_INT) !== false) {
                    continue;
                }
                return false;
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * @param $ids
     * @return bool
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 10:46
     */
    protected function enumIdsArr($ids): bool
    {
        try {
            if (count($ids) !== count(array_unique($ids))) {
                return false;
            }
            $ids = array_unique($ids);
            foreach ($ids as $id) {
                if (filter_var($id, FILTER_VALIDATE_INT) !== false) {
                    continue;
                }
                return false;
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * @param $time
     * @return bool
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 10:46
     */
    protected function enumYmdHis($time): bool
    {
        try {
            if (is_array($time)) {
                foreach ($time as $item) {
                    if (!self::enumYmdHis($item)) {
                        return false;
                    }
                }
                return true;
            }
            if (!date_format(date_create($time), 'Y-m-d H:i:s')) {
                return false;
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * @param $value
     * @return bool
     */
    protected function enumIdSearch($value): ?bool
    {
        try {
            if (false !== filter_var($value, FILTER_VALIDATE_INT)) {
                $rule = '1,1000000';
                if (is_string($rule)) {
                    $rule = explode(',', $rule);
                }
                [
                    $min,
                    $max,
                ] = $rule;

                return $value >= $min && $value <= $max;
            }
            if ($value === 'null') {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }

    protected function enumArray($value): ?bool
    {
        try {
            if (!is_array($value)) {
                return false;
            }
            foreach ($value as $item) {
                if (!$item) {
                    return false;
                }
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
}